package org.springblade.modules.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.InsuranceCompany;
import org.springblade.modules.admin.service.InsuranceCompanyService;
import org.springblade.modules.admin.vo.InsuranceCompanyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Date 11:20 2021/8/24
 * @Description {
 *   保险公司接口
 * }
 * @Author lsay
 **/
@RestController
@RequestMapping("/insurance")
public class InsuranceCompanyController {

	@Autowired
	private InsuranceCompanyService insuranceCompanyService;

	@GetMapping("/page")
	@TenantIgnore
	public R<IPage<InsuranceCompanyVO>> page(InsuranceCompany insuranceCompany, Query query){
		return R.data(insuranceCompanyService.selectInsuranceCompanyPage(Condition.getPage(query),insuranceCompany));
	}

	@GetMapping("/list")
	public R<List<InsuranceCompany>> list(){
		return R.data(insuranceCompanyService.list());
	}

	@PostMapping()
	public R<Boolean> add(@RequestBody InsuranceCompany insuranceCompany){
		int count = insuranceCompanyService.count(Wrappers.<InsuranceCompany>lambdaQuery()
			.eq(InsuranceCompany::getName,insuranceCompany.getName()));
		if(count > 0){
			throw new ServiceException("保险公司名称已被使用！");
		}
		return R.data(insuranceCompanyService.save(insuranceCompany));
	}

	@PutMapping("/{id}")
	public R<Boolean> update(@PathVariable Long id,@RequestBody InsuranceCompany insuranceCompany){
		insuranceCompany.setId(id);
		int count = insuranceCompanyService.count(Wrappers.<InsuranceCompany>lambdaQuery().ne(InsuranceCompany::getId,id)
			.eq(InsuranceCompany::getName,insuranceCompany.getName()));
		if(count > 0){
			throw new ServiceException("保险公司名称已被使用！");
		}
		return R.data(insuranceCompanyService.updateById(insuranceCompany));
	}

	@DeleteMapping()
	public R<Boolean> remove(@RequestParam String ids) {
		return R.status(insuranceCompanyService.removeByIds(Func.toLongList(ids)));
	}

}
